package com.ttai.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerSetMiYaoComponet;
import com.ttai.dagger.module.activity.SetMiYaoPresenterModule;
import com.ttai.presenter.activity.InsertTipAcountPresenter;
import com.ttai.presenter.activity.SetMiYaoPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.untils.AESUtil;
import com.ttai.untils.AesAndroid;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetMiyao extends BaseActivity {
    @Bind(R.id.ed_passwordtip)
    EditText edPasswordtip;
    @Bind(R.id.et_key)
    EditText etKey;
    @Bind(R.id.et_inputagain)
    EditText etInputagain;
    @Bind(R.id.btn_jiami)
    Button btnJiami;

    @Inject
    SetMiYaoPresenter setMiYaoPresenter;

//    InsertTipAcountPresenter insertTipAcountPresenter;

    private String acountId;
    private String acountPassword;
    public  String miYaoTip;
    private String miYao;
    private String miYaoAgain;
    private String jiamiAcountId;
    private String jiamiAcountPassword;
    public static String value;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setmiyao);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("账号密码加密");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ButterKnife.bind(this);
        setComponet();
        Intent intent=getIntent();
        acountId =intent.getStringExtra("acountId");
        acountPassword =intent.getStringExtra("acountPassword");

//        insertTipAcountPresenter = new InsertTipAcountPresenter();
//        insertTipAcountPresenter.storeTipAcount(Constant.getToken());
        value = intent.getStringExtra("value");


        if(!value.isEmpty()){
            edPasswordtip.setText(value);
            edPasswordtip.setInputType(InputType.TYPE_NULL);
        }
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void setComponet() {
        DaggerSetMiYaoComponet.builder()
                .setMiYaoPresenterModule(new SetMiYaoPresenterModule(this))
                .build()
                .in(this);
    }

    @OnClick(R.id.btn_jiami)
    public void onViewClicked() {


        miYaoTip =edPasswordtip.getText().toString();
        miYao =etKey.getText().toString();
        miYaoAgain =etInputagain.getText().toString();

        if((miYao ==null || miYao.isEmpty())||(miYaoAgain ==null || miYaoAgain.isEmpty() ) ||(miYaoTip ==null || miYaoTip.isEmpty() )){
            Toast.makeText(SetMiyao.this,"加密失败,提示或秘钥不能为空",Toast.LENGTH_SHORT).show();
        }
        else if (miYao.equals(miYaoAgain)){

            jiamiAcountId = Constant.string2Unicode(AesAndroid.encrypt(miYao, acountId));
            jiamiAcountPassword =Constant.string2Unicode(AesAndroid.encrypt(miYao, acountPassword));
//            jiamiAcountId = Constant.string2Unicode(new String(AESUtil.encrypt(this,acountId,miYao)));
//            jiamiAcountPassword = Constant.string2Unicode(new String(AESUtil.encrypt(this,acountPassword,miYao)));
//
//            System.out.println("jiamiAcountId :"+jiamiAcountId+ " jiamiAcountPassword :"+jiamiAcountPassword);
//
//            String xorAcount = new String(AESUtil.decrypt(this,Constant.unicode2String(jiamiAcountId).getBytes(),miYao));
//            String xorPassword = new String(AESUtil.decrypt(this,Constant.unicode2String(jiamiAcountPassword).getBytes(),miYao));
//            System.out.println("xorAcount:"+xorAcount+" xorPassword:"+xorPassword);

            token = Constant.getToken();
            try{
                setMiYaoPresenter.encrypt(token,AddAcount.acountName,jiamiAcountId,jiamiAcountPassword,1);
            }catch (Exception e){
                Toast.makeText(SetMiyao.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }

            /**
             * SHA256密钥加密
             */
            try{
                String sha256Password=Constant.shaEncrypt(miYao);
                setMiYaoPresenter.setKey(token,sha256Password);
            }catch (Exception e){
                Toast.makeText(SetMiyao.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }




            /**
             * 判断value是否存在
             */
            if(value==null || value.isEmpty()){
                Log.i("33", "onViewClicked: "+miYaoTip);
                setMiYaoPresenter.setKetTip(token,miYaoTip);
            }

            Toast.makeText(SetMiyao.this,"加密成功",Toast.LENGTH_SHORT).show();
            finish();
            Intent intent1=new Intent();
            intent1.setClass(SetMiyao.this,UaP_Protect.class);
            intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
            startActivity(intent1);
        }
        else {
            Toast.makeText(SetMiyao.this,"加密失败,秘钥不一致",Toast.LENGTH_SHORT).show();
        }
    }
}
